package com.jsgt.greekfire.ui.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.LogUtils
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.MenuEntity
import com.jsgt.greekfire.ui.home.adapter.MenuParentAdapter
import com.jsgt.greekfire.ui.home.adapter.MyAdapter
import com.jsgt.greekfire.ui.home.drag.DragCallback
import kotlinx.android.synthetic.main.activity_menu_manage.*
import kotlinx.android.synthetic.main.view_white_bg_title.*
import java.io.Serializable

class MenuManageActivity : BaseActivity() {
    private var indexSelect: ArrayList<MenuEntity> = ArrayList<MenuEntity>()
    private var indexDataList: ArrayList<MenuEntity> = ArrayList<MenuEntity>()
    private var menuList: ArrayList<MenuEntity> = ArrayList<MenuEntity>()
    private var appContext: App? = null
    private var adapterSelect: MyAdapter? = null
    private var menuParentAdapter: MenuParentAdapter? = null

    override fun getLayoutId(): Int = R.layout.activity_menu_manage

    override fun main() {
        appContext = application as App?
        title_tv.text = getString(R.string.menu_manage_title)
        right_tv?.visibility = View.VISIBLE
        right_tv?.setTextColor(resources.getColor(R.color.menu_right_tv_color))
        right_tv.text = getString(R.string.menu_manage_manage)
        right_tv?.setOnClickListener {
            if (right_tv?.text.toString() == getString(R.string.menu_manage_manage)) {
                right_tv?.text = getString(R.string.menu_manage_finish)
                adapterSelect!!.setEdit()
                if (menuParentAdapter != null) {
                    menuParentAdapter!!.setEdit()
                }
                tv_drag_tip.visibility = View.VISIBLE
            } else {
                right_tv?.text = getString(R.string.menu_manage_manage)
                tv_drag_tip.visibility = View.GONE
                adapterSelect!!.endEdit()
                if (menuParentAdapter != null) {
                    menuParentAdapter?.endEdit()
                }
                postMenu()
                finish()
            }
        }
        initView()
        initData()
    }

    protected fun postMenu() {
        if (appContext?.readObject(AppConfig.KEY_USER_TEMP) != null) {
            val indexDataList =
                appContext?.readObject(AppConfig.KEY_USER_TEMP) as List<MenuEntity>
            val key = AppConfig.KEY_USER
            appContext?.saveObject(indexDataList as Serializable, key)
        }
    }

    private fun initView() {
        //获取设置保存到本地的菜单
        //获取设置保存到本地的菜单
        if (appContext?.readObject(AppConfig.KEY_USER) != null) {
            indexDataList =
                appContext?.readObject(AppConfig.KEY_USER) as ArrayList<MenuEntity>
        }
        if (indexDataList != null) {
            indexSelect.clear()
            if (indexDataList.size > 7) {
                for (i in 0..6) {
                    indexSelect.add(indexDataList[i])
                }
            } else {
                indexSelect.addAll(indexDataList)
            }
        }
        adapterSelect =
            MyAdapter(this, appContext, indexSelect)
        gridview?.setAdapter(adapterSelect)

        gridview.setDragCallback(object : DragCallback {
            override fun startDrag(position: Int) {
                LogUtils.error("start drag at " + "" + position)
                sv_index.startDrag(position)
            }

            override fun endDrag(position: Int) {
                LogUtils.error("end drag at " + "" + position)
                sv_index.endDrag(position)
            }
        })
        gridview.setOnItemClickListener(AdapterView.OnItemClickListener { parent, view, position, id ->
            Log.e(
                "setOnItemClickListener",
                adapterSelect?.getEditStatue().toString() + ""
            )
            if (!adapterSelect?.getEditStatue()!!) {
                //dragGridView.clicked(position);
                val cateModel: MenuEntity = indexSelect.get(position)
                initUrl(cateModel)
            }
        })
        gridview.setOnItemLongClickListener(AdapterView.OnItemLongClickListener { parent, view, position, id ->
            if (right_tv?.text.toString() == getString(R.string.menu_manage_manage)) {
                right_tv?.text = getString(R.string.menu_manage_finish)
                adapterSelect?.setEdit()
                if (menuParentAdapter != null) {
                    menuParentAdapter?.setEdit()
                }
                tv_drag_tip.visibility = View.VISIBLE
            }
            gridview.startDrag(position)
            false
        })

    }

    private fun initData() {

        if (appContext?.readObject(AppConfig.KEY_All) != null) {
            var indexDataList =
                appContext!!.readObject(AppConfig.KEY_All) as ArrayList<MenuEntity>
            init(indexDataList)
        }
    }

    private fun init(indexAll: ArrayList<MenuEntity>) {
        expandableListView.setGroupIndicator(null)
        menuList.clear()
        try {
            var index = MenuEntity()
            index.title = getString(R.string.menu_manage_more)
            index.id = ("1")
            var indexLC: ArrayList<MenuEntity> = ArrayList()
            indexLC.addAll(indexAll)

            for (i in indexLC.indices) {
                for (j in indexSelect.indices) {
                    if (indexLC[i]!!.title
                            .equals(indexSelect[j].title)
                    ) {
                        indexLC[i]!!.select = (true)
                    }
                }
            }
            index.childs = (indexLC)
            menuList.add(index)

            menuParentAdapter =
                MenuParentAdapter(this@MenuManageActivity, menuList)
            expandableListView.setAdapter(menuParentAdapter)

            // expandableListView.expandGroup(6); // 在分组列表视图中 展开一组
            // expandableListView.isGroupExpanded(0); //判断此组是否展开
            for (i in 0 until menuParentAdapter!!.getGroupCount()) {
                expandableListView.expandGroup(i)
            }

            expandableListView.setOnGroupClickListener { expandableListView, view, groupPosition, l ->
                var cateModel: MenuEntity = menuList.get(groupPosition)
                true
            }

            expandableListView.onItemClickListener =
                AdapterView.OnItemClickListener { arg0, arg1, arg2, arg3 ->

                    if (right_tv?.getText().toString() == getString(R.string.menu_manage_manage)) {
                        var cateModel: MenuEntity = menuList.get(arg2)
                        initUrl(cateModel)
                    }
                }


            expandableListView.onItemLongClickListener =
                AdapterView.OnItemLongClickListener { arg0, arg1, arg2, arg3 ->
                    if (right_tv?.text.toString() == getString(R.string.menu_manage_manage)) {
                        right_tv?.text = getString(R.string.menu_manage_finish)
                        adapterSelect?.setEdit()
                        menuParentAdapter!!.setEdit()
                    }
                    false
                }


        } catch (e: Exception) {
        }
    }

    fun initUrl(cateModel: MenuEntity) {
        if (right_tv?.text.toString() == getString(R.string.menu_manage_manage)) {
            val intent = Intent()
            val bundle = Bundle()
            val title = cateModel.title
            val strId = cateModel.id
            //dian
            // showToast(title + "")
        }
    }


    fun delMenu(indexData: MenuEntity, position: Int) {

        for (i in menuList.indices) {
            for (k in menuList[i].childs!!.indices) {
                if (menuList[i].childs?.get(k)?.title
                        .equals(indexData.title)
                ) {
                    menuList[i].childs?.get(k)?.select = (false)
                }
            }
        }
        if (menuParentAdapter != null) {
            menuParentAdapter!!.notifyDataSetChanged()
        }
        adapterSelect!!.notifyDataSetChanged()
    }


    fun addMenu(context: Context?, menuEntity: MenuEntity?) {

        if (indexSelect.size < 7) {
            indexSelect.add(menuEntity!!)
            val key = AppConfig.KEY_USER_TEMP
            appContext!!.saveObject(
                indexSelect as Serializable?,
                key
            )
            for (i in menuList.indices) {
                for (k in menuList.get(i).childs!!.indices) {
                    if (menuList[i].childs?.get(k)?.title
                            .equals(menuEntity.title)
                    ) {
                        menuList[i].childs?.get(k)?.select = (true)
                    }
                }
            }
            menuParentAdapter!!.notifyDataSetChanged()
            adapterSelect!!.notifyDataSetChanged()
        } else {
            showToast(getString(R.string.menu_manage_max))
        }
    }

}